﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.SessionState;
using System.Windows.Forms;
using System.Web.UI.HtmlControls;
public partial class Login_LoginForm : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        

    }


    protected void Login_Click(object sender, EventArgs e)
    {
        string userName = txtUser.Text;
        string password = txtPass.Text;
        LINQDataContext lin = new LINQDataContext();
        IQueryable rg = null;
        rg = from a in lin.GetTable<Logins>()
             where a.UserName == userName
             && a.Pass == password
             select a;
        foreach (Logins item in rg)
        {

            if (item != null)
            {
                Session.SetCurrentUser(userName);
                if (Remember.Checked)
                {
                    HttpCookie cookie = new HttpCookie("UserName");
                    cookie.Value = userName;
                    cookie.Expires = DateTime.Now.AddDays(30);
                    Response.Cookies.Add(cookie);

                }
                else
                {
                    HttpCookie cookie = new HttpCookie("UserName");
                    cookie.Value = "NoOne";
                    cookie.Expires = DateTime.Now.AddDays(-1d);
                    Response.Cookies.Add(cookie);
                }

                string returnUrl = Request.QueryString["returnUrl"];
                if (!string.IsNullOrEmpty(returnUrl))
                    Response.Redirect(returnUrl);
                else Response.Redirect("~/HomePage.aspx");


            }
            else
            {
                Session.SetCurrentUser(null);
                Response.Redirect("~/Login/Login.aspx");
            }

        }
    }
}